package com.parking.service.impl;

import com.parking.bean.ParkSpace;
import com.parking.dao.ParkSpaceDao;
import com.parking.dao.impl.ParkSpaceDaoImpl;
import com.parking.service.ParkSpaceService;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ParkSpaceServiceImpl implements ParkSpaceService {
     private ParkSpaceDao parkSpaceDao=new ParkSpaceDaoImpl();
    @Override
    public List<ParkSpace> list() {
        return this.parkSpaceDao.list();
    }

    @Override
    public List<ParkSpace> search(String key, String value) {
        if (value.equals("")) return this.parkSpaceDao.list();
        return this.parkSpaceDao.search(key,value);
    }

    @Override
    public void save(ParkSpace parkSpace) {
        parkSpace.setParkSpaceState("空闲");
        Date date= new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        parkSpace.setParkSpaceCreateDate(simpleDateFormat.format(date));
        Integer save=this.parkSpaceDao.save(parkSpace);
        if (save!=1) {
            throw new RuntimeException("添加车位信息失败");
        }
    }

    @Override
    public void update(ParkSpace parkSpace) {
        Integer update=this.parkSpaceDao.update(parkSpace);
        if (update!=1) throw new RuntimeException("更新车位信息失败");
    }

    @Override
    public void delete(Integer id) {
        Integer delete=this.parkSpaceDao.delete(id);
        if (delete!=1) throw new RuntimeException("删除车位信息失败");
    }
}
